<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\File;
use think\Request;
class Mailbox extends Controller{
    public function mailbox(){//客房发布
        $list = Db::name('hotel')->where('type=0')->order('id desc')->paginate(6);
        $page = $list->render();
         return view('Mailbox/mailbox',array("data"=>$list,"page"=>$page));
    }
    public function select($mailbox_id){//模糊查询
        $get_id = $mailbox_id;
        $name = $_POST["keywords"];
        if($name==""){//判断name值是否为空
            $row = Db::name('room_release')->paginate(6);
//            $this->error('查询数据不能为空','index','',1);
        }else{                                                                                           //不否为空进行模糊查询
            $row = Db::table("room_release")->where("
                name like '%{$name}'
                or name like '%{$name}'
                or name like '%{$name}%'
                ")->paginate(6);  //查询 细分类的表 找到对应的类名
        }
        $page = $row->render();
        return view('mailbox_list',array('lists'=>$row,'page'=>$page,'mailbox_id'=>$get_id));
    }
    public function mailbox_list($mailbox_id){
        $get_id = $mailbox_id;
        $lists = Db::name('room_release')->where("hotel_id=$get_id")->order('id desc')->paginate(6);
        $cc = array();
        for($i=0;$i<count($lists);$i++){
            $cc[$i]['r_img'] = explode("/",$lists[$i]["r_img"]);
        }
        $page = $lists->render();
        return view('Mailbox/mailbox_list',array("cc"=>$cc,"lists"=>$lists,"page"=>$page,"mailbox_id"=>$get_id));
    }
    public function mailbox_add($mailbox_id){//客房发布方法
        $get_id = $mailbox_id;
        $lists = Db::name('hotel')->where("id=$get_id")->paginate(6);
        return view('Mailbox/mailbox_add',["bo"=>$lists,'mailbox_id'=>$get_id]);
    }
    public function mailbox_add_show($mailbox_id){//添加客房
        $get_id = $mailbox_id;
        $file = request()->file("file");
       if(request()->isPost()){//判断是否是post提交
        $cc="";
        foreach($file as $files) {
            $info = $files->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                $cc .= $info->getSaveName()."/" ;
                $img = substr($cc,0,strlen($cc)-1);
            } else {
               $this->success('添加失败');
            }
        }
            $data = array(
                'r_name'=>$_POST['Mailbox_name'],
                'hotel_id'=>$get_id,
                'r_img'=>$img,
                'r_introduce'=>$_POST['mailbox_introduction'],
                'amounts'=>$_POST['amounts'],
                'r_time'=>$_POST['mailbox_time'],
            );
            $test = Db::table("room_release")->where("id=$get_id")->insert($data);
            if(!$test){

            }else{
                $this->redirect('Mailbox/mailbox_list', ['mailbox_id'=>$get_id]);
            }
        }else{
            echo 0;
        }
    }
    public function mailbox_see($mailbox_id){//客房展示方法
        $get_id = $mailbox_id;
        $date = Db::table('hotel')
            ->alias('h')
            ->join('room_release r','h.id = r.hotel_id')
            ->where("r.id=$get_id")
            ->paginate(6);
//        dump($date);
        $cc = array();
        for($i=0;$i<count($date);$i++){
            $cc[$i]['r_img'] = explode("/",$date[$i]["r_img"]);
        }
        $page = $date->render();
        return view('mailbox_see',["cc"=>$cc,'date'=>$date,'page'=>$page,'mailbox_id'=>$get_id]);
    }
    public function modify($mailbox_id){//修改方法
        $get_id = $mailbox_id;
        $date = Db::table('hotel')
            ->alias('h')
            ->join('room_release r','h.id = r.hotel_id')
            ->where("r.id=$get_id")
            ->paginate(6);
//        dump($date);
        $cc = array();
        for($i=0;$i<count($date);$i++){
            $cc[$i]['r_img'] = explode("/",$date[$i]["r_img"]);
        }
        $page = $date->render();
        return view('Mailbox/modify',['cc'=>$cc,'date'=>$date,'page'=>$page,'mailbox_id'=>$get_id]);
    }
    public function modify_save($mailbox_id){//修改方法
        $get = $mailbox_id;
        $file = request()->file("file");
//        dump($file);
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        $img  = $info->getSaveName();
        $request = Request::instance();
        // print_r($request);
        $data =array(
            'r_name'=> $request->post('r_name'),
            'r_img'=>$img,
            'r_introduce'=>$request->post('merchant_introduction'),
        );
        $text = Db::table("room_release")->where("id=$get")->update($data);
        if(!$text){
            $this->error('修改失败','modify','',1);
        }else{
            $this->redirect('mailbox_see', ['mailbox_id'=>$get]);
        }
    }
    public function delete(){
        $get =$_GET['mailbox_id'];

        $data = Db::table('room_release')->where("id=$get")->delete();
        if (!$data){
            echo "0";
        }else{
            $this->redirect('mailbox_see', ['mailbox_id'=>$get]);
        }

//        try{
//            Db::table('room_release')->where("hotel_id=$get")->delete();
//            // 提交事务
//            Db::commit();
//            echo 'ok';
//        } catch (\Exception $e) {
//            // 回滚事务
//            echo "chack";
//            Db::rollback();
//        }
        $this->redirect('mailbox_see', ['mailbox_id'=>$get]);
    }

    public function mail_detail($mailbox_id){//特价客房发布
        $get_id =  $mailbox_id;
        dump($get_id);
        $list = Db::name('room_release')->where("id=$get_id")->paginate(6);//这查的是数
        $page = $list->render();
        return view("mail_detail",array("data"=>$list,"page"=>$page,['mailbox_id'=>$mailbox_id]));
//        return view('mail_detail');

    }
    public function mail_detail_add(){//特价客房发布方法

        return view('mail_detail_add');

    }
    public  function mailbox_order(){
//     return view("Tourism/tourism");
        $list = Db::name('guest_room')->paginate(5);
        $page = $list->render();
        return view("order",array("data"=>$list,"page"=>$page));

    }

}
